package com.retail.marketing.server.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.retail.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.retail.marketing.common.domain.TCoupon;
import com.retail.marketing.server.mapper.TCouponRuleMapper;
import com.retail.marketing.common.domain.TCouponRule;
import com.retail.marketing.server.service.ITCouponRuleService;

/**
 * 优惠券规则管理Service业务层处理
 *
 * @author retail
 * @date 2024-02-27
 */
@Service
public class TCouponRuleServiceImpl implements ITCouponRuleService
{
    @Autowired
    private TCouponRuleMapper tCouponRuleMapper;

    /**
     * 查询优惠券规则管理
     *
     * @param couponRuleId 优惠券规则管理主键
     * @return 优惠券规则管理
     */
    @Override
    public TCouponRule selectTCouponRuleByCouponRuleId(Integer couponRuleId)
    {
        return tCouponRuleMapper.selectTCouponRuleByCouponRuleId(couponRuleId);
    }

    /**
     * 查询优惠券规则管理列表
     *
     * @param tCouponRule 优惠券规则管理
     * @return 优惠券规则管理
     */
    @Override
    public List<TCouponRule> selectTCouponRuleList(TCouponRule tCouponRule)
    {
        return tCouponRuleMapper.selectTCouponRuleList(tCouponRule);
    }

    /**
     * 新增优惠券规则管理
     *
     * @param tCouponRule 优惠券规则管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTCouponRule(TCouponRule tCouponRule)
    {
        int rows = tCouponRuleMapper.insertTCouponRule(tCouponRule);
        insertTCoupon(tCouponRule);
        return rows;
    }

    /**
     * 修改优惠券规则管理
     *
     * @param tCouponRule 优惠券规则管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTCouponRule(TCouponRule tCouponRule)
    {
        tCouponRuleMapper.deleteTCouponByCouponId(tCouponRule.getCouponRuleId());
        insertTCoupon(tCouponRule);
        return tCouponRuleMapper.updateTCouponRule(tCouponRule);
    }

    /**
     * 批量删除优惠券规则管理
     *
     * @param couponRuleIds 需要删除的优惠券规则管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTCouponRuleByCouponRuleIds(Integer[] couponRuleIds)
    {
        tCouponRuleMapper.deleteTCouponByCouponIds(couponRuleIds);
        return tCouponRuleMapper.deleteTCouponRuleByCouponRuleIds(couponRuleIds);
    }

    /**
     * 删除优惠券规则管理信息
     *
     * @param couponRuleId 优惠券规则管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTCouponRuleByCouponRuleId(Integer couponRuleId)
    {
        tCouponRuleMapper.deleteTCouponByCouponId(couponRuleId);
        return tCouponRuleMapper.deleteTCouponRuleByCouponRuleId(couponRuleId);
    }

    /**
     * 新增优惠券管理信息
     *
     * @param tCouponRule 优惠券规则管理对象
     */
    public void insertTCoupon(TCouponRule tCouponRule)
    {
        List<TCoupon> tCouponList = tCouponRule.getTCouponList();
        Integer couponRuleId = tCouponRule.getCouponRuleId();
        if (StringUtils.isNotNull(tCouponList))
        {
            List<TCoupon> list = new ArrayList<>();
            for (TCoupon tCoupon : tCouponList) {
                tCoupon.setCouponId(couponRuleId);
                list.add(tCoupon);
            }
            if (list.size()>0){
                tCouponRuleMapper.batchTCoupon(list);
            }
        }
    }
}
